<?php
// +----------------------------------------------------------------------
// | ZanPianCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.zanpian.com All rights reserved.
// +----------------------------------------------------------------------
// | BBS:  <http://www.feifeicms.cc>
// +----------------------------------------------------------------------
//赞片CMS
namespace app\common\model;
use think\Model;
class Newsrel extends Model{	
	public function vod(){
        return $this->hasMany('vod','vod_id','newsrel_did')->where('newsrel_sid',1);
    }
	public function star(){
        return $this->hasMany('star','star_id','newsrel_did')->where('newsrel_sid',3);
    }
	public function newsrel_del($id='',$sid=''){
		if(!empty($id)){
            $where['newsrel_did']=$id;
		}
		if(!empty($sid)){
			$where['newsrel_sid']=$sid;
		}
		if(!empty($where)){
		   db('Newsrel')->where($where)->delete();	
		}	
	}
	public function newsrel_updata($id,$type='',$keyword=''){
		$keyarray = explode(',',str_replace(array('，','、'),',',$keyword));
		$keyarray=array_unique($keyarray);
        if($type==1){
			foreach($keyarray as $key=>$val){
			    $valarray=	explode('#',$val);
			    if(!empty($valarray[1]) && !empty($valarray[0])){
			        $data[$key]['newsrel_did'] = $valarray[1];
			        $data[$key]['newsrel_name']=$valarray[0];
			    }elseif(empty($valarray[1]) && !empty($valarray[0])){
			        $data[$key]['newsrel_did'] = db('vod')->where('vod_name',$valarray[0])->value('vod_id');	
			        $data[$key]['newsrel_name']=$valarray[0];
			    }elseif(!empty($valarray[1]) && empty($valarray[0])){
			        $data[$key]['newsrel_did'] = $valarray[1];	
			        $data[$key]['newsrel_name']=db('vod')->where('vod_id',$valarray[1])->value('vod_name');
			    }
				$data[$key]['newsrel_sid']=$type;
		        $data[$key]['newsrel_nid']=$id;
			}
			db('Newsrel')->where(array('newsrel_nid'=>$id,'newsrel_sid'=>$type))->delete();
		    if(isset($data)){
		        db('Newsrel')->insertAll($data);   
		    }
            unset($data);			
		}
        elseif($type==3){
			foreach($keyarray as $key=>$val){
			    $valarray=	explode('#',$val);
			    if(!empty($valarray[1]) && !empty($valarray[0])){
			        $data[$key]['newsrel_did'] = $valarray[1];
			        $data[$key]['newsrel_name']=$valarray[0];
			    }elseif(empty($valarray[1]) && !empty($valarray[0])){
			        $data[$key]['newsrel_did'] = db('star')->where('star_name',$valarray[0])->value('star_id');	
			        $data[$key]['newsrel_name']=$valarray[0];
			    }elseif(!empty($valarray[1]) && empty($valarray[0])){
			        $data[$key]['newsrel_did'] = $valarray[1];	
			        $data[$key]['newsrel_name']=db('star')->where('star_id',$valarray[1])->value('star_name');
			    }
				$data[$key]['newsrel_sid']=$type;
		        $data[$key]['newsrel_nid']=$id;
			}
			db('Newsrel')->where(array('newsrel_nid'=>$id,'newsrel_sid'=>$type))->delete();
		    if(isset($data)){
		        db('Newsrel')->insertAll($data);   
		    }
            unset($data);			
		}else{
			foreach($keyarray as $key=>$val){
			    $vodid=db('vod')->where('vod_name',$val)->value('vod_id');
			    $starid=db('star')->where('star_name',$val)->value('star_id');
			    if(!empty($vodid)){
			        $data[$key]['newsrel_did'] = $vodid;
					$data[$key]['newsrel_sid'] = 1;
			    }elseif(!empty($starid)){
			        $data[$key]['newsrel_did'] = $starid;
					$data[$key]['newsrel_sid'] = 3;
			    }else{
			        $data[$key]['newsrel_did'] = 0;	
					$data[$key]['newsrel_sid'] = 0;	
			        
			    }
				$data[$key]['newsrel_name']=$val;
				$data[$key]['newsrel_nid']=$id;
				db('Newsrel')->where($data[$key])->delete();
			}
		    if(isset($data)){
		        db('Newsrel')->insertAll($data);   
		    }
            unset($data);			
			
			
		}		
		
   }	
}